﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace spaceVibration
{
    class ConfigClass
    {
        public  string inipath;

        //[DllImport("kernel32.dll")]

        //private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //[DllImport("kernel32.dll")]

        //private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        /// <summary>

        /// 构造方法

        /// </summary>

        /// <param name="INIPath">文件路径</param>

        public ConfigClass(string INIPath)
        {

            inipath = INIPath;

        }

        /// <summary>

        /// 写入INI文件

        /// </summary>

        /// <param name="Section">项目名称(如 [TypeName] )</param>

        /// <param name="Key">键</param>

        /// <param name="Value">值</param>

        public void IniWriteValue(string Section, string Key, string Value)
        {

            string[] s = new string[200];
            string[] k = new string[200];
            string[] Section1 = new string[1000];
            string[] Section2 = new string[1000];
            string[,] arr = new string[30, 30];
            if (ExistINIFile())
            {
                
                string nextLine;
      
                FileStream fs = new FileStream(@inipath, FileMode.Truncate,FileAccess.Write);

                StreamReader sr = new StreamReader(fs);
                StreamWriter sw=new StreamWriter(fs);
                while ((nextLine = sr.ReadLine()) != null)
                {
                    nextLine = nextLine.Replace(" ", "");
                    if (!nextLine.Contains("#") && nextLine != "")
                    {
                        if (nextLine.Contains("[" + Section + "]") || nextLine.Contains(Key + "="))
                        {
                            if (nextLine.Contains(Key))
                            {

                                k = nextLine.Split(new char[] { '=' });
                                nextLine =  Key + "=" + Value;
                            }

                        }
                    }
                    sw.WriteLine(nextLine);
                }
                sr.Close();

            }
            //WritePrivateProfileString(Section, Key, Value, this.inipath);
        }

        /// <summary>

        /// 读出INI文件

        /// </summary>

        /// <param name="Section">项目名称(如 [TypeName] )</param>

        /// <param name="Key">键</param>

        public string[] IniReadValue(string Section, string Key)//arr[0]section  arr[1] 是key  arr[2] 为value
        {
            string[] s = new string[200];
            string[] k = new string[200];
            string[] Section1 = new string[1000];
            string[] Section2 = new string[1000];
            string[] Key1 = new string[1000];
            string[] value = new string[1000];
            string[] value1 = new string[1000];
            string[,] arr = new string[30, 30];
            if (ExistINIFile())
            {
                StreamReader sr = new StreamReader(@inipath, Encoding.Default);
                string nextLine;
                int i = 0;

                while ((nextLine = sr.ReadLine()) != null)
                {
                    nextLine = nextLine.Replace(" ", "");
                    if (!nextLine.Contains("#") && nextLine != "")
                    {
                        if (nextLine.Contains("[" + Section + "]") || nextLine.Contains(Key + "="))
                        {
                            if (nextLine.Contains(Key))
                            {

                                k = nextLine.Split(new char[] { '=' });
                                value[i] = k[1];
                                i++;
                            }

                        }
                    }

                }
                sr.Close();

            }
            return value;
        }

        /// <summary>

        /// 验证文件是否存在

        /// </summary>

        /// <returns>布尔值</returns>

        public bool ExistINIFile()
        {

            return File.Exists(inipath);

        }
    }
}
